//
//  LoginViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/18.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "HostLoginViewController.h"
#import "HostHomeViewController.h"
#import "AppDelegate.h"
#import <IDLFaceSDK/FaceSDKManager.h>
#import "LivenessViewController.h"
#import "LivingConfigModel.h"
#import "Host_IndexViewController.h"
#import "RegistFViewController.h"
#import "HostRegiViewController.h"
#import "DetectionViewController.h"
//#import "BloginViewController.h"

//#import "RegistFViewController.h"
@interface HostLoginViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *balid_Btn;
@property (weak, nonatomic) IBOutlet UITextField *pass_Tf;
@property (weak, nonatomic) IBOutlet UILabel *role_Name;
@property (weak, nonatomic) IBOutlet UITextField *mob_Tf;
@property (weak, nonatomic) IBOutlet UIScrollView *botom_ScroView;
@property (weak, nonatomic) IBOutlet UILabel *valid_text;
@property (weak, nonatomic) IBOutlet UIButton *type_Btn;
@property(assign)BOOL validType;
@property (nonatomic,assign) RoleType roleType;

@end

@implementation HostLoginViewController


- (instancetype)initWithRoleType:(RoleType)roleType
{
    if(self = [super init]){
        _roleType = roleType;
    }
    return self;
}

    

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"" andNaviColor:nil andTitleColor:nil];
    self.balid_Btn.hidden = 1;
    self.mob_Tf.delegate = self;
    self.botom_ScroView.contentSize = CGSizeMake(screen_width, screen_height*1.2);
    self.botom_ScroView.delegate = self;
    self.botom_ScroView.showsVerticalScrollIndicator = 0;
  
    switch (_roleType) {
        case RoleType_YH:
            self.role_Name.text = @"用户登录";
            break;
        case RoleType_ZB:
            self.role_Name.text = @"主播登录";
            break;
        case RoleType_SJ:
            self.role_Name.text = @"商户登录";
            self.mob_Tf.keyboardType =  UIKeyboardTypeDefault;
            break;
        case RoleType_GHS:
            self.role_Name.text = @"供货商登录";
            break;
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKb)];
    [self.view addGestureRecognizer:tap];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


#pragma mark--登录
- (IBAction)login:(UIButton *)sender {
    [self.view endEditing: 1];
    if([self.mob_Tf.text isEqualToString:@""]||[self.pass_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请填写完整的登录信息"];
        return;
    }
    //    dict[@"account"] = self.mob_Tf.text;
    //    dict[@"password"] = self.password_tf.text;
    //    dict[@"ac"] = @"auth";
    //    dict[@"op"] = @"login";
    //    dict[@"ac"] = @"auth";
    //    dict[@"ta"] = @"merch_login";
    NSDictionary *params;
    switch (_roleType) {
        case RoleType_YH:
            params=   @{@"ctrl":@"live",@"ac":@"auth",@"op":@"login",@"mobile":self.mob_Tf.text,@"password":self.pass_Tf.text};
            break;
        case RoleType_ZB:
            params = @{@"ctrl":@"live_anchor",@"ac":@"auth",@"op":@"login",@"mobile":self.mob_Tf.text,@"password":self.pass_Tf.text,@"ta":@"index"};
            break;
        case RoleType_SJ:
            params = @{@"ctrl":@"live_merch",@"ac":@"auth",@"op":@"login",@"account":self.mob_Tf.text,@"password":self.pass_Tf.text,@"ta":@"merch_login"};
            break;
        case RoleType_GHS:
#warning 测试数据
            params=   @{@"ctrl":@"live",@"ac":@"auth",@"op":@"login",@"mobile":self.mob_Tf.text,@"password":self.pass_Tf.text};
            break;
    }
    
    
    [self weqPostHandle:params success:^(id response) {
        if(response){
            NSDictionary *userInfo = [NSDictionary changeType:response[@"data"]];
            [self loginSuccessWithUserInfo:userInfo];
        }
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark--登录成功后的处理
- (void)loginSuccessWithUserInfo:(id)userInfo
{

    [XTool SaveDefaultInfo:@(_roleType) Key:Prole];
    
    switch (_roleType) {
        case RoleType_YH:
            [XTool SaveDefaultInfo:userInfo Key:USER_INFO];
            break;
        case RoleType_ZB:
            [XTool SaveDefaultInfo:userInfo Key:HOST_USER];
            [[NSNotificationCenter defaultCenter]postNotificationName:STOPTIMENOTO object:nil];
            break;
        case RoleType_SJ:
            [XTool SaveDefaultInfo:userInfo Key:BUSSION_INFO];
            [[NSNotificationCenter defaultCenter]postNotificationName:STOPTIMENOTO object:nil];
            break;
        case RoleType_GHS:
            [XTool SaveDefaultInfo:userInfo Key:G_BUSSION_INFO];
            [[NSNotificationCenter defaultCenter]postNotificationName:STOPTIMENOTO object:nil];
            break;
    }
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app changeRootWithRoleType:_roleType];
}




//忘记密码
- (IBAction)forgetPass:(UIButton *)sender {
    [self.view endEditing:1];
}

//显示密码
- (IBAction)showPassword:(UIButton *)sender {
    self.pass_Tf.secureTextEntry = 0;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if (textField == self.mob_Tf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.mob_Tf.text.length >= 11) {
            self.mob_Tf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}


-(void)hideKb{
    [self.view endEditing:1];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:1];
}



////切换登录方式
//- (IBAction)gotoregist:(UIButton *)sender
//{
//    if(self.validType==0){
//        self.validType = 1;
//        self.valid_text.text = @"验证码登录";
//        self.pass_Tf.placeholder = @"请输入验证码";
//         [self.type_Btn setTitle:@"密码登录" forState:UIControlStateNormal];
//        self.balid_Btn.hidden = 0;
//    }else{
//        self.validType = 0;
//        self.valid_text.text = @"密码登录";
//        [self.type_Btn setTitle:@"验证码登录" forState:UIControlStateNormal];
//        self.pass_Tf.placeholder = @"输入密码，长度不小于6位";
//        self.balid_Btn.hidden = 0;
//    }
//
//}
//
//
//
//- (IBAction)getValid:(UIButton *)sender
//{
//
//    if([self.mob_Tf.text isEqualToString:@""]){
//        [WToast showWithText:@"请输入正确的手机号"];
//        return;
//    }
//    NSString *md5Str = [NSString stringWithFormat:@"km%@bz",self.mob_Tf.text];
//    NSString *tokens = [XTool md5:md5Str];
//    NSDictionary *param = @{@"ctrl":@"common",@"mobile":self.mob_Tf.text,@"captcha":tokens,@"ac":@"code"};
//    [self weqPostHandle:param success:^(id response) {
//
//        if(response){
//            [WToast showWithText:@"发送成功"];
//        }
//    } failure:^(NSError *error) {
//
//    }];
//}
//
//
//#pragma mark-- 人脸登录
//- (IBAction)face_Login:(UIButton *)sender
//{
//    LivenessViewController* lvc = [[LivenessViewController alloc] init];
//    LivingConfigModel* model = [LivingConfigModel sharedInstance];
//    NSArray *arr = @[@(FaceLivenessActionTypeLiveEye)];
//    model.isByOrder = YES;
//    [model.liveActionArray addObjectsFromArray:arr];
//    [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
//    lvc.completion=^(NSDictionary *images, UIImage *originImage){
//        if([images[@"bestImage"] count] > 0){
//            NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//            NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//            //self.base_Str = base_Str;
//            [self uploadFace:base_Str];
//            NSLog(@"base-------%@",base_Str);
//        }
//    };
//    [self pushSecondVC:lvc];
////    DetectionViewController* dvc = [[DetectionViewController alloc] init];
////    dvc.completion=^(NSDictionary *images, UIImage *originImage){
////        NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
////
////        NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
////        //self.base_Str = base_Str;
////        [self uploadFace:base_Str];
////        NSLog(@"base-------%@",base_Str);
////    };
////    [self pushSecondVC:dvc];
//}
//
//
//-(void)uploadFace:(NSString *)base64
//{
//    NSDictionary *param = @{@"image":base64,@"ta":@"face_login",@"op":@"login",@"ctrl":@"live_anchor",@"ac":@"auth"};
//    [self weqPostHandle:param success:^(id response) {
//        if(response){
//
//            [XTool SaveDefaultInfo:response[@"data"] Key:HOST_USER];
//            [XTool SaveDefaultInfo:@(_roleType) Key:Prole];
//            [WToast showWithText:@"登录成功"];
//            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//            [app changeRootWithRoleType:_roleType];
//         }
//    } failure:^(NSError *error) {
//
//    }];
//}
//
//
//- (IBAction)regist:(UIButton *)sender {
//    [self.view endEditing:1];
//    HostRegiViewController *vc = [[HostRegiViewController alloc]init];
//    [self pushSecondVC:vc];
//}
@end
